package models

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type GroupMemberExtendModel interface {
	Count(ctx context.Context, groupId int64) (int64, error)
	FindList(ctx context.Context, groupId, offset, limit int64) ([]*GroupMember, error)
}

func NewGroupMemberExtendModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) GroupMemberExtendModel {
	return &defaultGroupMemberModel{
		CachedConn: sqlc.NewConn(conn, c, opts...),
		table:      "`group_member`",
	}
}

func (m *defaultGroupMemberModel) Count(ctx context.Context, groupId int64) (int64, error) {
	var count int64
	query := fmt.Sprintf("select count(*) from %s where `group_id` = ? and status = 0", m.table)
	err := m.QueryRowNoCacheCtx(ctx, &count, query, groupId)
	return count, err
}

func (m *defaultGroupMemberModel) FindList(ctx context.Context, groupId, offset, limit int64) ([]*GroupMember, error) {
	var list []*GroupMember
	query := fmt.Sprintf("select %s from %s where `group_id` = ? and status = 0 and id > ? offset ?", groupMemberRows, m.table)
	err := m.QueryRowsNoCacheCtx(ctx, &list, query, groupId, limit, offset)
	return list, err
}
